/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package scrumprojekt;

import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
import java.io.*;
import javax.swing.JOptionPane;

/**
 *
 * @author Kristoffer
 */
public class mail {
    
  private static final String SMTP_HOST_NAME = "send.one.com";
  private static final String SMTP_AUTH_USER = "scrum@kmdev.se";
  private static final String SMTP_AUTH_PWD  = "scrum2012";

  private static final String emailMsgTxt      = "Du har ansökningar att godkänna på vits utbildning!";
  private static final String emailSubjectTxt  = "Ansökningar att godkänna, VITS";
  private static final String emailFromAddress = "scrum@kmdev.se";

  // Add List of Email address to who email needs to be sent to
  private static final String[] emailList = {"kristoffermatsson@gmail.com"};

  public static void main(String args[]) throws Exception
  {
    mail smtpMailSender = new mail();
    smtpMailSender.postMail();
    
  }

  public void postMail( ) throws MessagingException
  {
    boolean debug = false;

     //Set the host smtp address
     Properties props = new Properties();
     props.put("mail.smtp.host", SMTP_HOST_NAME);
     props.put("mail.smtp.auth", "true");

    Authenticator auth = new SMTPAuthenticator();
    Session session = Session.getDefaultInstance(props, auth);

    session.setDebug(debug);

    // create a message
    Message msg = new MimeMessage(session);

    // set the from and to address
    InternetAddress addressFrom = new InternetAddress(emailFromAddress);
    msg.setFrom(addressFrom);

    InternetAddress[] addressTo = new InternetAddress[emailList.length];
    for (int i = 0; i < emailList.length; i++)
    {
        addressTo[i] = new InternetAddress(emailList[i]);
    }
    msg.setRecipients(Message.RecipientType.TO, addressTo);


    // Setting the Subject and Content Type
    msg.setSubject(emailSubjectTxt);
    msg.setContent(emailMsgTxt, "text/plain");
    Transport.send(msg);
    JOptionPane.showMessageDialog(null, "Din rapport är sparad och en notis om godkännande har blivit sänd till närmsta chef per mail");
 }


/**
* SimpleAuthenticator is used to do simple authentication
* when the SMTP server requires it.
*/
private class SMTPAuthenticator extends javax.mail.Authenticator
{

    public PasswordAuthentication getPasswordAuthentication()
    {
        String username = SMTP_AUTH_USER;
        String password = SMTP_AUTH_PWD;
        return new PasswordAuthentication(username, password);
    }
}

    
}
